from PySide6.QtCore import Qt, Signal
from PySide6.QtWidgets import QHBoxLayout, QWidget
from qfluentwidgets import SettingCard, FluentIconBase
from qfluentwidgetspro import ScreenColorPicker
from app.common.config import cfg

class ScreenColorPickerCard(SettingCard):
    """ 屏幕取色器设置卡片 """
    
    colorChanged = Signal(str)  # 发送颜色值的信号

    def __init__(self, icon: FluentIconBase, title: str, content: str, 
                 configItem, parent=None):
        super().__init__(icon, title, content, parent)
        self.configItem = configItem  # 颜色的配置项
        
        # 创建屏幕取色器
        self.colorPicker = ScreenColorPicker(self)
        
        # 设置初始颜色值
        self.colorPicker.setColor(self.configItem.value)
        
        # 设置最小宽度
      
        
        # 将颜色选择器添加到右侧
        self.hBoxLayout.addWidget(self.colorPicker, 0, Qt.AlignRight)
        
        # 连接信号
        self.colorPicker.colorChanged.connect(self._onColorChanged)

    def _onColorChanged(self, color: str):
        """ 颜色改变时的处理 """
        self.configItem.value = color
        cfg.save()
        self.colorChanged.emit(color)

    def getColor(self):
        """ 获取当前颜色值 """
        return self.colorPicker.color()

    def setColor(self, color: str):
        """ 设置颜色值 """
        self.colorPicker.setColor(color) 